package com.powerunion.datacollection.web.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionRedirect;
import org.apache.struts.actions.DispatchAction;

import com.powerunion.datacollection.config.ConfigUtil;
import com.powerunion.datacollection.model.User;
import com.powerunion.datacollection.service.UserManager;
import com.powerunion.util.log.LogHelper;

public class UserACLAction extends DispatchAction {

	private static final Logger logger = LogHelper.getInstance().getLogger(UserACLAction.class);

	public UserACLAction() {
		super();
	}
	
	public void finalize() throws Throwable {
		super.finalize();
	}
	
	public ActionForward managerIndex(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

        String userCode = request.getRemoteUser();
        String configUser = ConfigUtil.getInstance().getParameter("system.manager.name");
        if(!userCode.equals(configUser)) {
        	return mapping.findForward("right.deny");
        }
        
        UserManager um = new UserManager();
        User user = um.getUser(userCode);
        
        //request.getSession(false).setAttribute("userName", user.getUserName());
        request.getSession(false).setAttribute("userName", "管理员");

		return mapping.findForward("managerIndex.view");
	}
	
	public ActionForward userIndex(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
        String userCode = request.getRemoteUser();
        String configUser = ConfigUtil.getInstance().getParameter("system.manager.name");
        if(userCode.equals(configUser)) {
        	return mapping.findForward("right.deny");
        }
        
        UserManager um = new UserManager();
        User user = um.getUser(userCode);
        String userName = "";
        if(user != null){
        	userName = user.getUserName();
        }else{
        	userName = "未知";
        }
        request.getSession(false).setAttribute("userName", userName);
		return mapping.findForward("userIndex.view");
	}
	
	public ActionForward logout(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		session.removeAttribute("sysName");
		session.removeAttribute("sysId");
		session.removeAttribute("sysUserCode");
		request.getSession().removeAttribute("userName");
		session.invalidate();

		ActionRedirect redirect = new ActionRedirect(mapping
				.findForward("back.index"));
		return redirect;
	}
	
}
